﻿using Microsoft.AspNetCore.Mvc;
using ZR.Admin.WebApi.Filters;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;
using MiniExcelLibs;

//创建时间：2025-09-08
namespace ZR.Admin.WebApi.Controllers {
    /// <summary>
    /// 报警信息配置
    /// </summary>
    [Verify]
    [Route("business/CksAlarmMessageConfig")]
    [ApiExplorerSettings(GroupName = "business")]
    public class CksAlarmMessageConfigController : BaseController {
        /// <summary>
        /// 报警信息配置接口
        /// </summary>
        private readonly ICksAlarmMessageConfigService _CksAlarmMessageConfigService;

        public CksAlarmMessageConfigController(ICksAlarmMessageConfigService CksAlarmMessageConfigService) {
            _CksAlarmMessageConfigService = CksAlarmMessageConfigService;
        }

        /// <summary>
        /// 查询报警信息配置列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "cksalarmmessageconfig:list")]
        public IActionResult QueryCksAlarmMessageConfig([FromQuery] CksAlarmMessageConfigQueryDto parm) {
            var response = _CksAlarmMessageConfigService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询报警信息配置详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "cksalarmmessageconfig:query")]
        public IActionResult GetCksAlarmMessageConfig(int Id) {
            var response = _CksAlarmMessageConfigService.GetInfo(Id);

            var info = response.Adapt<CksAlarmMessageConfigDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加报警信息配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "cksalarmmessageconfig:add")]
        [Log(Title = "报警信息配置", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCksAlarmMessageConfig([FromBody] CksAlarmMessageConfigDto parm) {
            var modal = parm.Adapt<CksAlarmMessageConfig>().ToCreate(HttpContext);

            var response = _CksAlarmMessageConfigService.AddCksAlarmMessageConfig(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新报警信息配置
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "cksalarmmessageconfig:edit")]
        [Log(Title = "报警信息配置", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCksAlarmMessageConfig([FromBody] CksAlarmMessageConfigDto parm) {
            var modal = parm.Adapt<CksAlarmMessageConfig>().ToUpdate(HttpContext);
            var response = _CksAlarmMessageConfigService.UpdateCksAlarmMessageConfig(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除报警信息配置
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "cksalarmmessageconfig:delete")]
        [Log(Title = "报警信息配置", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCksAlarmMessageConfig([FromRoute] string ids) {
            var idArr = Tools.SplitAndConvert<int>(ids);

            return ToResponse(_CksAlarmMessageConfigService.Delete(idArr));
        }

        /// <summary>
        /// 导出报警信息配置
        /// </summary>
        /// <returns></returns>
        [Log(Title = "报警信息配置", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "cksalarmmessageconfig:export")]
        public IActionResult Export([FromQuery] CksAlarmMessageConfigQueryDto parm) {
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _CksAlarmMessageConfigService.ExportList(parm).Result;
            if (list == null || list.Count <= 0) {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "报警信息配置", "报警信息配置");
            return ExportExcel(result.Item2, result.Item1);
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="formFile"></param>
        /// <returns></returns>
        [HttpPost("importData")]
        [Log(Title = "报警信息配置导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false)]
        [ActionPermissionFilter(Permission = "cksalarmmessageconfig:import")]
        public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile) {
            List<CksAlarmMessageConfigDto> list = new();
            using (var stream = formFile.OpenReadStream()) {
                list = stream.Query<CksAlarmMessageConfigDto>(startCell: "A1").ToList();
            }

            return SUCCESS(_CksAlarmMessageConfigService.ImportCksAlarmMessageConfig(list.Adapt<List<CksAlarmMessageConfig>>()));
        }

        /// <summary>
        /// 报警信息配置导入模板下载
        /// </summary>
        /// <returns></returns>
        [HttpGet("importTemplate")]
        [Log(Title = "报警信息配置模板", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [AllowAnonymous]
        public IActionResult ImportTemplateExcel() {
            var result = DownloadImportTemplate(new List<CksAlarmMessageConfigDto>() { }, "CksAlarmMessageConfig");
            return ExportExcel(result.Item2, result.Item1);
        }

    }
}